<template>
  <BasicContainer>
    <div class="avue-crud">
      <ElForm ref="queryForm" :model="queryParams" :inline="true">
        <ElFormItem label="部门名称" prop="deptName">
          <ElInput
            v-model="queryParams.deptName"
            placeholder="请输入部门名称"
            clearable
            size="small"
            @keyup.enter.native="getList" />
        </ElFormItem>
        <ElFormItem>
          <ElButton type="primary" icon="el-icon-search" @click="getList">搜索</ElButton>

          <ElButton
            v-if="permissions.sys_menu_add"
            icon="el-icon-plus"
            type="primary"
            @click="addOrUpdateHandle(false)">
            添加
          </ElButton>

          <ElDropdown v-if="connectSync" style="margin-left: 10px">
            <ElButton type="primary"> 互联同步<i class="el-icon-arrow-down el-icon--right"></i> </ElButton>
            <template #dropdown>
              <ElDropdownMenu>
                <ElDropdownItem @click.native="sync(1)">钉钉部门同步</ElDropdownItem>
                <ElDropdownItem @click.native="sync(2)">钉钉用户同步</ElDropdownItem>
                <ElDropdownItem @click.native="sync(3)">企微部门同步</ElDropdownItem>
                <ElDropdownItem @click.native="sync(4)">企微用户同步</ElDropdownItem>
              </ElDropdownMenu>
            </template>
          </ElDropdown>
        </ElFormItem>
      </ElForm>

      <ElTable
        v-loading="loading"
        border
        :data="deptList"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'children' }">
        <ElTableColumn prop="name" label="部门名称" :show-overflow-tooltip="true" />
        <ElTableColumn prop="weight" label="排序" align="center" />
        <ElTableColumn prop="createTime" label="创建时间" align="center" />
        <ElTableColumn label="操作" align="center" width="200">
          <template #default="scope">
            <ElButton
              v-if="permissions.sys_dept_add"
              size="small"
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false, scope.row.id)">
              新增
            </ElButton>
            <ElButton
              v-if="permissions.sys_dept_edit"
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="addOrUpdateHandle(true, scope.row.id)">
              修改
            </ElButton>
            <ElButton
              v-if="permissions.sys_dept_del"
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)">
              删除
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>

      <TableForm v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList" />
    </div>
  </BasicContainer>
</template>

<script>
  import { delObj, fetchTree, syncCpDept, syncCpUser, syncDept, syncUser } from '@/api/admin/dept'
  import TableForm from './dept-form'
  import { mapState } from 'vuex'
  import { connectSync } from '@/config'

  export default {
    name: 'Dept',
    components: { TableForm },
    data() {
      return {
        connectSync,
        addOrUpdateVisible: false,
        // 遮罩层
        loading: true,
        // 表格树数据
        deptList: [],
        // 查询参数
        queryParams: {
          deptName: ''
        }
      }
    },
    computed: {
      ...mapState('user', ['permissions'])
    },
    created() {
      this.getList()
    },
    methods: {
      addOrUpdateHandle(isEdit, id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(isEdit, id)
        })
      },
      getList() {
        this.loading = true
        fetchTree(this.queryParams).then(response => {
          this.deptList = response.data.data
          this.loading = false
        })
      },
      handleDelete(row) {
        this.$confirm(`是否确认删除名称为"${row.name}"的数据项?`, '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            return delObj(row.id)
          })
          .then(() => {
            this.getList()
            this.$message.success('删除成功')
          })
      },
      // 互联数据同步
      sync(type) {
        this.$confirm('此操作将覆盖现有系统部门人员数据，请做好数据备份, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(() => {
            if (type === 1) {
              return syncDept()
            }
            if (type === 2) {
              return syncUser()
            }

            if (type === 3) {
              return syncCpDept()
            }

            if (type === 4) {
              return syncCpUser()
            }
          })
          .then(() => {
            this.$message.success('操作成功')
            this.getList()
          })
      }
    }
  }
</script>
